package com.taoyibao.hongcanting.ui.restaurant.delegate;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.kymjs.frame.view.AppDelegate;
import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.event.EventGift;
import com.taoyibao.hongcanting.model.GiftInfoModel;
import com.taoyibao.hongcanting.ui.restaurant.area.ItemGiftArea;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description:
 * @date :${DATA} 16:14
 */
public class GiftPageFragmnetDelegate extends AppDelegate implements MultiItemTypeAdapter.OnItemClickListener {
    private RecyclerView mRecyclerView;
    private List<GiftInfoModel> mData;
    private CommonAdapter mAdapter;

    @Override
    public int getRootLayoutId() {
        return R.layout.fragment_gift_page;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        mRecyclerView = get(R.id.rv_gift_page);
        mData = new ArrayList<>();
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(mAdapter = new CommonAdapter<GiftInfoModel>(getActivity(), R.layout.item_gift_shell, mData) {
            @Override
            protected void convert(ViewHolder holder, GiftInfoModel giftInfoModel, final int position) {
                ItemGiftArea itemGiftArea = holder.getView(R.id.layout_item_gift_shell);
                itemGiftArea.setData(giftInfoModel);
            }
        });
        mAdapter.setOnItemClickListener(this);
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(List<GiftInfoModel> data) {
        if (data == null || data.size() < 1) {
            return;
        }
        mData.clear();
        mData.addAll(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        //更新数据源
        for (int i = 0; i < mData.size(); i++) {
            if (i == position) {
                mData.get(i).isCheckd = true;
            } else {
                mData.get(i).isCheckd = false;
            }
        }
        mAdapter.notifyDataSetChanged();
        //发送消息目的：dialog页面收消息用于发送按钮使用
        EventGift eventGift = new EventGift();
        eventGift.type = 1;
        eventGift.mGiftInfoModel = mData.get(position);
        EventBus.getDefault().post(eventGift);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
